package com.ideal.gpt.app.local;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.lzwork.utility.TimeTicker;
import com.lzwork.utility.counter.ItemCounter;
import com.lzwork.utility.officeparser.ExcelReader.Line;
import com.lzwork.utility.officeparser.ExcelReader.XlsData;
import com.lzwork.utility.officeparser.ExcelReaderEv;
import com.lzwork.utility.officeparser.ExcelSaver;
import com.lzwork.utility.str.StrUtility;

/**
 * @author LiZheng
 * @date 2023年12月26日 14:41:19
 */

public class TypeCombine extends ExcelSaver
{
	final static String KEY_CONTENT = "诉求内容";

	final static String[] KEY_TYPES =
	{ "内容分类1", "内容分类2", "内容分类3", "内容分类4" };

	final static String KEY_ADDR = "诉求地址";
	final static String KEY_SENT_TO = "实际工单派单方向";
	final static String KEY_CORRECT = "提示是否正确";

	public static void combine(File input, File output)
	{
		TimeTicker ticker = new TimeTicker();
		XlsData pages = ExcelReaderEv.readData(input);

		ItemCounter<String> typeCount = new ItemCounter<>();

		System.out.println("Load input done. Cost " + ticker.tickS());

		Workbook wb = createXlsFile(output);

		Sheet sheet = wb.createSheet();

		appendLine(sheet, KEY_CONTENT, "TYPE_IN_4", KEY_ADDR, KEY_SENT_TO, KEY_CORRECT);

		List<Line> allLines = pages.allLines();
		int total = allLines.size();
		int num = 0;

		int skipShort = 0;
		int writeNum = 0;
		int skipDup = 0;

		ticker.clear();
		for (Line line : allLines)
		{
			if (num > 0 && num % 1000 == 0)
				System.out.println(num + "/" + total + " items checked.");

			num++;

			String content = line.getFieldData(KEY_CONTENT);
			if (content.length() < 20)
			{
				skipShort++;
				continue;
			}

			List<String> types = new ArrayList<>();
			for (int i = 0; i < KEY_TYPES.length; i++)
			{
				types.add(line.getFieldData(KEY_TYPES[i]));
			}
			String fullType = StrUtility.combineString(types, "_");

			int nowNum = typeCount.getCountNum(fullType);
			if (nowNum >= 2)
			{
				skipDup++;
				continue;
			}

			typeCount.addItem(fullType);

			appendLine(sheet, content, fullType, line.getFieldData(KEY_ADDR), line.getFieldData(KEY_SENT_TO),
					line.getFieldData(KEY_CORRECT));
			writeNum++;
		}

		System.out.println("All done. Cost " + ticker.tickS());
		System.out.println("Write content: " + writeNum + " items.");
		System.out.println("Skip too short: " + skipShort + " items.");
		System.out.println("Skip full type: " + skipDup + " items.");

		ticker.clear();
		saveXlsFile(wb, output);
		System.out.println("Save done. Cost " + ticker.tickS());
	}

	public static void main(String[] args)
	{
		File input = new File("v:/12345/智能派单202309_总.xlsx");
		File output = new File("v:/12345/智能派单202309_总_in4.xlsx");

		combine(input, output);
	}
}
